﻿#include "loadinglabel.h"

LoadingLabel::LoadingLabel(QWidget *parent) : QLabel(parent)
{
    //设置动画图片路径
    setPixmapPath(":/img/loading/loading", 8);
}

// 获取图像序号
int LoadingLabel::pixmap_() const
{
    return this->m_nIndex;
}

// 切换图标
void LoadingLabel::setPixmap_index(const int &index)
{
    m_nIndex = index;
    //qDebug()<<m_pixmapPath + QString("%1.png").arg(index);
    QPixmap pixmap(m_pixmapPath + QString("%1.png").arg(index));
    this->setPixmap(pixmap);
}

//设置要播放的图片路径和数量
void LoadingLabel::setPixmapPath(const QString &pixmapPath, int pixmapCnt)
{
    m_pixmapPath = pixmapPath;
    m_pixmapCnt = pixmapCnt;
}

//开启动画
void LoadingLabel::startAnimation_()
{
    m_pAnimation = new QPropertyAnimation(this, "pixmap_");
    m_pAnimation->setDuration(1000);
    // 永远运行，直到stop
    m_pAnimation->setLoopCount(-1);

    // 设置动画各区间的值
    for (int i = 0; i < 8; ++i)
    {
        m_pAnimation->setKeyValueAt(i/7.0, i + 1);
    }

    this->m_pAnimation->start();
}

//关闭动画
void LoadingLabel::stopAnimation_()
{
    this->m_pAnimation->stop();
}
